﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyCongDan.GUI
{
    public partial class frmMauDonViTuExcel : DevExpress.XtraEditors.XtraForm
    {
        private string maDonVi = string.Empty;
        private Dictionary<int, DTO.DTO_Field> dictFieldValue = new Dictionary<int, DTO.DTO_Field>();

        public string MaDonVi
        {
            get { return maDonVi; }
            set { maDonVi = value; }
        }
        
        public frmMauDonViTuExcel()
        {
            InitializeComponent();
            InitColumnValue();
            InitDonViField();
        }

        private void InitDonViField()
        {
            comboBoxField.DataSource = DAL.DAL_Field.GetListField();
            comboBoxField.DisplayMember = "TenField";
            comboBoxField.ValueMember = "MaField";
        }

        private void InitColumnValue()
        {
            comboBoxColumn.Items.Add("Không chọn");
            for (int i = 0; i < 15; i++)
            {
                comboBoxColumn.Items.Add((char)('A' + i));
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog of = new OpenFileDialog();
                of.Filter = "Excel 2003|*.xls";
                if (of.ShowDialog() == DialogResult.OK)
                {
                    txtDuongDan.Text = of.FileName;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Có lỗi xảy ra");
                //btnNhapLieu.Enabled = false;

            }
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDongDauTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void btnNhapLieu_Click(object sender, EventArgs e)
        {
            try
            {
                DAL.DAL_DonVi.UpdateDonVi(maDonVi, Int32.Parse(txtDongDauTien.Text), Int32.Parse(txtDongCuoiCung.Text), txtDuongDan.Text);
                DAL.DAL_DonVi.InsertFieldDonViList(maDonVi, new List<DTO.DTO_Field>(dictFieldValue.Values));
                MessageBox.Show("Thành công");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void comboBoxField_SelectionChangeCommitted(object sender, EventArgs e)
        {
            UpdateFieldValueInfo();
        }

        private void UpdateFieldValueInfo()
        {
            listBoxFieldColumnValue.Items.Clear();
            foreach (DTO.DTO_Field curField in dictFieldValue.Values)
            {
                listBoxFieldColumnValue.Items.Add(curField.TenField + " : " + Convert.ToChar(curField.Value + 'A'));
            }
        }

        private void comboBoxColumn_SelectionChangeCommitted(object sender, EventArgs e)
        {
            
            DTO.DTO_Field curField = (DTO.DTO_Field)comboBoxField.SelectedItem;
            if (dictFieldValue.ContainsKey(curField.MaField) == false)
            {
                if (comboBoxColumn.SelectedIndex != 0)
                {
                    curField.Value = (char)comboBoxColumn.SelectedItem - 'A';
                    dictFieldValue.Add(curField.MaField, curField);
                }
            }
            else
            {
                if (comboBoxColumn.SelectedIndex != 0)
                {
                    dictFieldValue[curField.MaField].Value = (char)comboBoxColumn.SelectedItem - 'A';
                }
                else
                {
                    dictFieldValue.Remove(curField.MaField);
                }
            }
            UpdateFieldValueInfo();
        }

        private void frmMauDonViTuExcel_Load(object sender, EventArgs e)
        {
            this.textEditDonVi.Text = maDonVi;
        }
    }
}